﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using DevComponents.DotNetBar.Controls;

namespace Waiter.Controler
{
    class StaffCtrl
    {
        CafeDBDataContext dataContext = new CafeDBDataContext();

        #region data access methods

        /// <summary>
        /// Get tất cả danh sách nhân viên
        /// </summary>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public List<STAFF> fsGetAllStaffs()
        {
            List<STAFF> resultList = new List<STAFF>();

            try
            {
                resultList = CommonGlobalVariable.globalDataContext.STAFFs.Where(x => x.DEL_FLG == false).OrderBy(x => x.NAME).ToList<STAFF>();

                return resultList;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsGetAllStaffs",
                           "",
                           ex.ToString());
                return null;
            }            
        }

        /// <summary>
        /// Get tất cả danh sách nhân viên
        /// </summary>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public List<STAFF> fsGetAllStaffs_CalcSalary()
        {
            List<STAFF> resultList = new List<STAFF>();

            try
            {
                resultList = CommonGlobalVariable.globalDataContext.STAFFs.Where(x => x.DEL_FLG == false & x.IS_SALARY_MANAGEMENT == true).OrderBy(x => x.NAME).ToList<STAFF>();

                return resultList;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsGetAllStaffs_CalcSalary",
                           "",
                           ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// Load data trạng thái nhân viên vào ComboBox của grid
        /// </summary>
        /// <param name="cmb"></param>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public ResultStatus fsLoadDataGridViewCombobox_WorkStatus(DataGridViewComboBoxExColumn cmb)
        {
            List<MASTER> resultList = new List<MASTER>();

            try
            {
                resultList = CommonGlobalVariable.globalDataContext.MASTERs.Where(x => x.GROUP_CODE == CommonConstants.TRANG_THAI_NHAN_VIEN & x.DEL_FLG == false).OrderBy(x => x.VALUE).ToList<MASTER>();
                cmb.DataSource = resultList;

                cmb.ValueMember = "CODE";
                cmb.DisplayMember = "VALUE";
                cmb.DataPropertyName = "STATUS_CODE";

                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsLoadDataGridViewCombobox_WorkStatus",
                           "",
                           ex.ToString());
                cmb.DataSource = null; 
            }

            return ResultStatus.ERROR;
        }

        /// <summary>
        /// Load data trạng thái nhân viên vào ComboBox của grid
        /// </summary>
        /// <param name="cmb"></param>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public ResultStatus fsLoadComboBox_WorkStatus(ComboBoxEx cmb)
        {
            List<MASTER> resultList = new List<MASTER>();

            try
            {
                resultList = CommonGlobalVariable.globalDataContext.MASTERs.Where(x => x.GROUP_CODE == CommonConstants.TRANG_THAI_NHAN_VIEN & x.DEL_FLG == false).OrderBy(x => x.VALUE).ToList<MASTER>();
                cmb.DataSource = resultList;

                cmb.ValueMember = "CODE";
                cmb.DisplayMember = "VALUE";

                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsLoadComboBox_WorkStatus",
                           "",
                           ex.ToString());
                cmb.DataSource = null;
            }

            return ResultStatus.ERROR;
        }

        /// <summary>
        /// Load data ten nhan vien vao commbobox
        /// </summary>
        /// <param name="cmb"></param>
        /// <returns></returns>
        public ResultStatus fsLoadComboBox_ListStaff(ComboBoxEx cmb)
        {
            List<STAFF> resultList = new List<STAFF>();

            try
            {
                resultList = CommonGlobalVariable.globalDataContext.STAFFs.Where(x => x.DEL_FLG == false).OrderBy(x=>x.NAME).ToList();
                cmb.DataSource = resultList;

                cmb.ValueMember = "CODE";
                cmb.DisplayMember = "NAME";

                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsLoadComboBox_ListStaff",
                           "",
                           ex.ToString());
                cmb.DataSource = null;
            }

            return ResultStatus.ERROR;
        }

        ///// <summary>
        ///// Load data trạng thái nhân viên vào ComboBox
        ///// </summary>
        ///// <param name="cmb"></param>
        ///// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        ///// <returns></returns>
        //public ResultStatus fsLoadCombobox_WorkStatus(ComboBoxEx cmb)
        //{
        //    List<MASTER> resultList = new List<MASTER>();
        //    //cmb.Items.Clear();

        //    try
        //    {
        //        resultList = CommonGlobalVariable.globalDataContext.MASTERs.Where(x => x.GROUP_CODE == CommonConstants.TRANG_THAI_NHAN_VIEN & x.DEL_FLG == false).OrderBy(x => x.VALUE).ToList<MASTER>();
        //        cmb.DataSource = resultList;

        //        cmb.ValueMember = "CODE";
        //        cmb.DisplayMember = "VALUE";

        //        return ResultStatus.SUCCESS;
        //    }
        //    catch (Exception)
        //    {
        //        return ResultStatus.ERROR;
        //    }
        //}

        /// <summary>
        /// Thêm mới data
        /// </summary>
        /// <param name="pCode"></param>
        /// <param name="pName"></param>
        /// <param name="pSex"></param>
        /// <param name="pBirthDay"></param>
        /// <param name="pPhoneNumber"></param>
        /// <param name="pAddress"></param>
        /// <param name="pStatusCode"></param>
        /// <param name="pIsSalaryManagement"></param>
        /// <param name="pUnitPrice"></param>
        /// <param name="pNote"></param>
        /// <param name="pOPID"></param>
        /// <param name="pAddDate"></param>
        /// <param name="pUpdDate"></param>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public ResultStatus fsInsertData(string pCode, string pName, bool pSex, DateTime pBirthDay, string pPhoneNumber, string pAddress, string pStatusCode, bool pIsSalaryManagement, long pUnitPrice, string pNote, string pOPID, DateTime pAddDate, DateTime pUpdDate)
        {
            STAFF insertData = new STAFF();
            try
            {
                //Tại phương thức này sẽ thêm mới data (không kiểm tra error)
                insertData.CODE = pCode;
                insertData.NAME = pName;
                insertData.SEX = pSex;
                insertData.BIRTH_DAY = pBirthDay;
                insertData.PHONE_NUMBER = pPhoneNumber;
                insertData.ADDRESS = pAddress;
                insertData.STATUS_CODE = pStatusCode;
                insertData.IS_SALARY_MANAGEMENT = pIsSalaryManagement;
                insertData.UNIT_SALARY = pUnitPrice;
                insertData.NOTE = pNote;
                insertData.DEL_FLG = false;
                insertData.OPID = pOPID;
                insertData.ADD_DATE = pAddDate;
                insertData.UPD_DATE = pUpdDate;

                CommonGlobalVariable.globalDataContext.STAFFs.InsertOnSubmit(insertData);
                CommonGlobalVariable.globalDataContext.SubmitChanges();

                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsInsertData",
                           CommonUtils.fsParamListToString(new object[1] { insertData }),
                           ex.ToString());
                return ResultStatus.ERROR;              
            }
        }

        /// <summary>
        /// Cập nhật data
        /// </summary>
        /// <param name="pCode"></param>
        /// <param name="pName"></param>
        /// <param name="pSex"></param>
        /// <param name="pBirthDay"></param>
        /// <param name="pPhoneNumber"></param>
        /// <param name="pAddress"></param>
        /// <param name="pStatusCode"></param>
        /// <param name="pIsSalaryManagement"></param>
        /// <param name="pUnitPrice"></param>
        /// <param name="pNote"></param>
        /// <param name="pOPID"></param>
        /// <param name="pUpdDate"></param>
        /// <history>Verion: 1.0.0.0 - TRAN_DANG_QUANG</history>
        /// <returns></returns>
        public ResultStatus fsUpdateData(string pCode, string pName, bool pSex, DateTime pBirthDay, string pPhoneNumber, string pAddress, string pStatusCode, bool pIsSalaryManagement, long pUnitPrice, string pNote, string pOPID, DateTime pUpdDate)
        {
            try
            {
                ////Tại phương thức này sẽ chỉ thực hiện update data (không kiểm tra error)

                var result = CommonGlobalVariable.globalDataContext.STAFFs.SingleOrDefault(x => x.CODE == pCode);

                if (result.CODE != String.Empty && result.CODE != "")//Không trả về trị mặc định (có data)
                {
                    result.NAME = pName;
                    result.SEX = pSex;
                    result.BIRTH_DAY = pBirthDay;
                    result.PHONE_NUMBER = pPhoneNumber;
                    result.ADDRESS = pAddress;
                    result.STATUS_CODE = pStatusCode;
                    result.IS_SALARY_MANAGEMENT = pIsSalaryManagement;
                    result.UNIT_SALARY = pUnitPrice;
                    result.NOTE = pNote;
                    result.OPID = pOPID;
                    result.UPD_DATE = pUpdDate;

                    CommonGlobalVariable.globalDataContext.SubmitChanges();
                    return ResultStatus.SUCCESS;
                }

                return ResultStatus.ERROR;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsUpdateData",
                           CommonUtils.fsParamListToString(new object[12] { pCode, pName, pSex, pBirthDay, pPhoneNumber, pAddress, pStatusCode, pIsSalaryManagement, pUnitPrice, pNote, pOPID, pUpdDate }),
                           ex.ToString());
                return ResultStatus.ERROR;
            }
        }

        /// <summary>
        /// Xóa một nhân viên (setting cờ xóa DELETE_FLG = true)
        /// </summary>
        /// <param name="pID"></param>
        /// <returns></returns>
        public ResultStatus fsDeleteStaff(string pID, string pOPID, DateTime pUpdDate)
        {
            try
            {
                var myStaff = CommonGlobalVariable.globalDataContext.STAFFs.SingleOrDefault(x => x.CODE == pID);

                if (myStaff.CODE == pID)//Truy vấn data thành công (không phải là trị mặc định)
                {
                    myStaff.DEL_FLG = true;
                    myStaff.OPID = pOPID;
                    myStaff.UPD_DATE = pUpdDate;
                    CommonGlobalVariable.globalDataContext.SubmitChanges();

                    return ResultStatus.SUCCESS;
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsDeleteStaff",
                           CommonUtils.fsParamListToString(new object[3] { pID, pOPID, pUpdDate }),
                           ex.ToString());
            }

            return ResultStatus.ERROR;
        }
        
        #endregion


        public STAFF fsFindStaffByCode(string code)
        {
            try
            {
                return CommonGlobalVariable.globalDataContext.STAFFs.FirstOrDefault(q=>q.CODE == code);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "StaffCtrl.fsFindStaffByCode",
                           CommonUtils.fsParamListToString(new object[1] { code }),
                           ex.ToString());
            }
            return null;
        }
    }
}
